package com.dfbz.controller.login;

import com.dfbz.controller.BaseServlet;
import com.dfbz.model.User;
import com.dfbz.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {
    UserServiceImpl userService=new UserServiceImpl();
    public void toLogin(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        try {
            req.setCharacterEncoding("UTF-8");
            resp.setCharacterEncoding("UTF-8");
            String username=req.getParameter("form-username");
            String password=req.getParameter("form-password");
            String checkCode=req.getParameter("checkCode");
            String sessionCode=(String) req.getSession().getAttribute("sessionCode");
            if (checkCode.equals(sessionCode)){
                User user=userService.findByName(username);
                if (null !=user){
                    if (password.equals(user.getPassword())){
                        //登录成功
                        Date date = new Date();
                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String dateStr = sdf.format(date);
                        User user1=new User();
                        user1.setId(user.getId());
                        user1.setLoginTime(date);
                        userService.updateLoginTime(user1);
                        req.getSession().setAttribute("loginTime",dateStr);
                        req.getSession().setAttribute("userInfo",user);
                        req.getRequestDispatcher("/view/location/home").forward(req,resp);
                        return;
                    }
                    else {
                        //密码错误
                        req.getRequestDispatcher("/view/location/login").forward(req,resp);
                        return;
                    }
                }
                else {
                    //账号不存在
                    req.getRequestDispatcher("/view/location/login").forward(req,resp);
                    return;
                }
            }
            else {
                //验证码错误
                req.getRequestDispatcher("/view/location/login").forward(req,resp);
                return;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void register(HttpServletRequest req, HttpServletResponse resp){
        try {
            HashMap<String, String> parm = getParm(req);
            String email=req.getParameter("form-email");
            String username=req.getParameter("form-username");
            String password=req.getParameter("form-password");
            String password2=req.getParameter("form-password2");
            if (null != userService.findByName(username)){
                //用户名存在
                req.getRequestDispatcher("/view/location/register").forward(req,resp);
                return;
            }
            else {
                User user = new User();
                user.setUsername(username);
                user.setEmail(email);
                user.setPassword(password);
                ArrayList<User> list = userService.findList();
                User user1 = list.get(list.size() - 1);
                Integer id = user1.getId();
                user.setId(id+1);
                userService.register(user);
                req.getRequestDispatcher("/view/location/login").forward(req,resp);
                return;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void forget(HttpServletRequest req, HttpServletResponse resp){

    }
    public void logout(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getSession().removeAttribute("userInfo");
            req.getRequestDispatcher("/view/location/login").forward(req,resp);
            return;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
